<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Response;
use app\models\LoginForm;
use app\models\ContactForm;
use app\services\CommandService;

class HomeController extends AuthController {
    public $currentHeader = 'header-site';
    public $currentNav = 'home-nav';
    public $pageTitle = "";

    public function init() {
        parent::init();
        $account = $this->service->getAdervertiserAccount();
        $this->account = $account;
        $this->user = $this->M('User', $this->S('user_id'));
    }

    public function actionDashboard() {
        $this->pageTitle = 'Dashboard';
        $this->brandShow = true;
        if ($this->S('advertiser_id')) {
            $this->advertiser = $this->M('Advertiser', $this->S('advertiser_id'));
        } else {
            $this->advertiser = [];
        }
        $filter = $this->isAllowedSeeShows();
        $this->filter = $filter;
        return $this->show();
    }

    public function actionUserUpdate() {
        $this->currentHeader = 'header-settings';
        $this->currentNav = 'modify-pwd-nav';
        $this->pageTitle = $this->t('home', 'Update Password');
        return $this->show();
    }

    public function actionUserUpdateHandle() {
        $us = new \app\services\UsersService();
        $password = $this->I('user_pwd');
        $confirm = $this->I('confirm_pwd');
        if ($us->updatePassword($password, $confirm)) {
            $this->OK($this->t('home', 'Password update successfully!'));
        }
        $this->NG($this->t('home', 'Password update failed!'));
    }

    public function actionGetAdvertiserData() {
        $advertiserId = $this->S('advertiser_id');
        $dayType = $this->I('type');
        $data = $this->service->getAdvertiserReportOnDateType($advertiserId, $dayType);
        $this->jsonReturn(['graph' => $data]);
    }
}// end of home controller
